package com.nf.test.utils;/*
 *@Author: gjh
 *Date: 2025/1/816:12
 *Describe:
 */

import java.sql.*;

public class DBUtil {
    private static final String Driver = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/jdbc_test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    private static final String UserName = "root";
    private static final String PassWord = "root";

    static {
        // 加载驱动程序
        try {
            Class.forName(Driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Failed to load MySQL JDBC driver", e);
        }
    }

    /**
     * 获取连接对象
     */
    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL, UserName, PassWord);
        } catch (SQLException e) {
            throw new RuntimeException("Failed to get database connection", e);
        }
    }

    /**
     * 返回结果集
     *
     * @param sql  SQL语句
     * @param objs 注入的参数
     * @return 结果集
     */
    public static ResultSet executeQuery(String sql, Object[] objs) {
        Connection connection = getConnection();
        try {
            if (objs == null) {
                Statement statement = connection.createStatement();
                return statement.executeQuery(sql);
            } else {
                PreparedStatement pstmt = connection.prepareStatement(sql);
                for (int i = 0; i < objs.length; i++) {
                    pstmt.setObject(i + 1, objs[i]);
                }
                return pstmt.executeQuery();
            }
        } catch (SQLException e) {
            close(null, null, connection); // 关闭连接
            throw new RuntimeException("Failed to execute query", e);
        }
    }

    /**
     * 返回受影响的行数
     *
     * @param sql  SQL语句
     * @param objs 注入的参数
     * @return 受影响的行数
     */
    public static int executeUpdate(String sql, Object[] objs) {
        Connection connection = getConnection();
        try {
            if (objs == null) {
                Statement statement = connection.createStatement();
                return statement.executeUpdate(sql);
            } else {
                PreparedStatement pstmt = connection.prepareStatement(sql);
                for (int i = 0; i < objs.length; i++) {
                    pstmt.setObject(i + 1, objs[i]);
                }
                return pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            close(null, null, connection); // 关闭连接
            throw new RuntimeException("Failed to execute update", e);
        } finally {
            close(null, null, connection); // 确保连接关闭
        }
    }

    /**
     * 关闭相关的JDBC对象
     *
     * @param rst  结果集
     * @param stmt 语句对象
     * @param conn 连接对象
     */
    public static void close(ResultSet rst, Statement stmt, Connection conn) {
        if (rst != null) {
            try {
                rst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试数据库连接
     */
    public static void main(String[] args) {
        try (Connection conn = getConnection()) {
            System.out.println("Database connection successful: " + conn);
        } catch (SQLException e) {
            throw new RuntimeException("Failed to connect to database", e);
        }
    }
}